﻿

namespace Huirui.Cavan.Domain.MainBoundedContext.SystemModule
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Huirui.Cavan.Core.Persistent;
    using Huirui.Cavan.Domain.MainBoundedContext.SystemModule;
    using Huirui.Cavan.Domain.MainBoundedContext.SearchModule;
    using Huirui.Cavan.Core.MyLinq;
    using Huirui.Cavan.Core.Extension;

    public class AreaSpecification : ISpecification<Area, AreaSearch>
    {
        public AreaSpecification()
        {
            this.SearchParams = new AreaSearch();
        }

        public AreaSearch SearchParams
        {
            get;
            set;
        }

        public System.Linq.Expressions.Expression<Func<Area, bool>> Expression
        {
            get
            {
                IQueryBuilder<Area> queryBuilder = QueryBuilder.Create<Area>();

                if (null != SearchParams)
                {
                    if (SearchParams.ID.IsNotEmpty())
                    {
                        queryBuilder = queryBuilder.Equals(a => a.Id.Substring(1, SearchParams.ID.Length), SearchParams.ID);
                    }
                    if (SearchParams.Spell.IsNotEmpty())
                    {
                        queryBuilder = queryBuilder.Equals(a => a.Spell, SearchParams.Spell);
                    }
                    if (SearchParams.ParentCode.IsNotEmpty())
                    {
                        queryBuilder = queryBuilder.Equals(a => a.ParentCode, SearchParams.ParentCode);
                        //queryBuilder = queryBuilder.NotEquals(a => a.Id, SearchParams.ParentCode);
                        //queryBuilder = queryBuilder.Equals(a => a.Id.Substring(1, SearchParams.ParentCode.Length), SearchParams.ParentCode);
                    }
                    if (SearchParams.Level > 0)
                    {
                        queryBuilder = queryBuilder.Equals(a => a.Level, SearchParams.Level);
                    }
                }

                return queryBuilder.Expression.Expand();
            }
        }
    }
}
